package com.guard.ui;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.Toast;

import com.guard.R;
import com.guard.android.Constants;
import com.guard.net.UpdateStatusController;
import com.guard.structures.SystemStatus;

public abstract class AbstractActivity extends FragmentActivity {

    protected static int sysStatusCode;
    protected static SystemStatus sysStatus;
    protected static boolean isNeedKillApp = false;
    protected UpdateStatusController mUpdateStatusController;
    private DialogFragment mDialogFragment;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        initUpdateStatusController();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mUpdateStatusController.startUpdateThread(0);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mUpdateStatusController.cancelUpdateTask();
    }

    protected void initUpdateStatusController() {
        mUpdateStatusController = new UpdateStatusController(this);
        mUpdateStatusController.setAutorefreshEnabled(true);
        mUpdateStatusController.setUpdateInterval(getResources().getInteger(R.integer.status_update_interval));
    }

    public void onUpdateStatus(SystemStatus status) {
        if (status != null) {
            sysStatusCode = status.getStatus().getCode();
            sysStatus = status;
            switch (status.getStatus()) {
            case noAlarm:
                if (this instanceof RegisterActivity || this instanceof PaymentWebViewActivity) {
                    return;
                }
                onStartActivity(MainActivity.class, null);
//                onStartActivity(MapGuardActivity.class, null);
                break;
            case orderReceived:
                if (this instanceof RegisterActivity || this instanceof PaymentWebViewActivity) {
                    return;
                }
                onStartActivity(InformationActivity.class, null);
                break;
            case goingPatrol:
                if (this instanceof RegisterActivity || this instanceof PaymentWebViewActivity) {
                    return;
                }
                onStartActivity(MapGuardActivity.class, null);
                break;
            case patrolArrived:
                if (this instanceof RegisterActivity || this instanceof PaymentWebViewActivity) {
                    return;
                }
                onStartActivity(MapGuardActivity.class, null);
                break;
            }
        }
    }

    protected void onStartActivity(Class<?> arg0, String message) {

        Class<? extends AbstractActivity> arg1 = this.getClass();

        if (arg0.equals(arg1)) {
            return;
        }

        Intent intent = new Intent(getApplicationContext(), arg0);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        if (message != null) {
            intent.putExtra(Constants.MESSAGE, message);
        }

        startActivity(intent);
    }

    public void showConnectionErrorDialog() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (mDialogFragment == null) {
            mDialogFragment = ConnectionErrorDialogFragment.newInstance(getResources().getString(R.string.error_message));
        }

        if (mDialogFragment.getDialog() == null) {
            mDialogFragment.show(ft, Constants.CONNECTION_ERROR_DIALOG_TAG);
        }
    }

    protected void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }

    protected ProgressDialog getProgressDialog(String text, boolean isCancelable) {
        ProgressDialog dialog = new ProgressDialog(this);
        dialog.setMessage(text);
        dialog.setIndeterminate(true);
        dialog.setMax(100);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setCancelable(isCancelable);
        return dialog;
    }

}
